package postpc.powerreminders.com.location;

import postpc.powerreminders.com.db.RotgDatabase;
import postpc.powerreminders.com.events.RotgEventManager;
import postpc.powerreminders.com.ui.activities.MainActivity;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class RotgLocationManager {

	// Singleton stuff
	private static RotgLocationManager m_Instance;

	private static final String TAG = "ROTG_Location_Manager";
	private static final long DEFAULT_SCAN_TIME = 3000;

	/*
	 * Reference to the Event Manager. 
	 */
	RotgEventManager eventManager;

	/*
	 * Reference to the WiFi controller.
	 */
	WIFIController wifiController;

	/*
	 * Reference to the GPS controller.
	 */
	GPSController gpsController;

	/*
	 * reference to the power manager.
	 */
	RotgPowerManager powerManager;

	/*
	 * Reference to the database.
	 */
	RotgDatabase dataBase;

	/*
	 * Reference to the movement detector.
	 */
	MovmentDetector movmentDetector;

	/*
	 * Reference to the main activity context for user alerts and services. 
	 */
	Context context;

	/**
	 * 
	 * @param em
	 * @param ma
	 * @param db
	 */
	public RotgLocationManager(RotgEventManager em, MainActivity ma, RotgDatabase db) {

		this.eventManager = em;
		this.dataBase = db;
		this.context = ma;
		m_Instance = this;

		/**
		 * Location Services
		 */
		Log.i(TAG, "Startin Location services");

		/*
		 * Start power manager
		 */
		this.powerManager = new RotgPowerManager();

		/*
		 * Start WiFi controller 
		 */	
		Log.i(TAG, "Starting WIFI Controller");
		this.wifiController = new WIFIController(this, powerManager, dataBase);
		this.wifiController.resetScanInterval(powerManager.getWifiScanInterval());

		/*
		 * Start GPS Controller
		 */
		Log.i(TAG, "Starting GPS Activity");
		this.gpsController = new GPSController(this, powerManager, dataBase);

		/*
		 * Starting Movement sensing
		 */
		this.movmentDetector = new MovmentDetector(this, this.powerManager);
	}

	/**
	 * 
	 */
	public void notifyMovment() {
		Log.i(TAG, "MOVMENT NOTIFICATION");

		// WiFi movement Notification
		this.powerManager.setWifiScanInterval(DEFAULT_SCAN_TIME);
		this.wifiController.resetScanInterval(this.powerManager.getWifiScanInterval());

		// GPS movment notification
		this.powerManager.GPSEnable();
		this.gpsController.requestGPSLocation();
	}

	/**
	 *
	 */
	public static RotgLocationManager getInstance() {
		return m_Instance;
	}

	/**
	 * 
	 */
	public void onDestroy() {
		// Destroying WiFi
		if (wifiController != null)
			wifiController.disableScaningAbility();

		// Destroying GPS 
		if (gpsController != null)
			gpsController.removeGPSUpdates();

		// Destroying Movement Sensor
		if (movmentDetector != null)
			movmentDetector.shutdown();
	}

	private String getMessageForLocation(Cursor cursor){
		Cursor result;
		String message = null;
		int active = 0;
		
		// Pull reminder message
		try {
			 result = this.dataBase.fetchReminderbyLocation(
					cursor.getLong((cursor.getColumnIndexOrThrow(RotgDatabase.KEY_ROWID))));
			 if (RotgDatabase.ACTION_LOCATION != result.getInt((result.getColumnIndexOrThrow(RotgDatabase.ROTG_ACTION)))){
				 Log.i(TAG, "Not an Action location");
				 return null;
			 }
			 
			 // If active show
			 active = result.getInt(result.getColumnIndexOrThrow(RotgDatabase.ACTIVE));
			 if (RotgDatabase.REMINDER_ACTIVE == active){
				 message = result.getString((result.getColumnIndexOrThrow(RotgDatabase.MESSAGE)));
			 }
			 
		}catch (Exception e){
			Log.e("IMPORTANT", "Error pulling message form location " + e.getMessage());
			return null;
		}
		return message;
	}
	
	/**
	 * 
	 * @param cursor
	 */
	void notifyEventWifi(Cursor cursor) {
		String message = getMessageForLocation(cursor);
		if (null != message){
			this.eventManager.notifyEventWifi(message);
		}
	}

	/**
	 * 
	 * @param string
	 */
	public void notifyUserDialog(String string) {
		this.eventManager.notifyUserDialog(string);		
	}

	/**
	 * 
	 * @param cursor
	 */
	public void notifyEventGPS(Cursor cursor) {
		String message = getMessageForLocation(cursor);
		if (null != message){
			this.eventManager.notifyReminderGPS(message);
		}
	}

	/**
	 * 
	 * Callback for the request current location
	 *
	 */
	public interface LocationManagerCallback {
		
		public void gpsInfoAvailable(double lon, double lat);

		public void wifiInfoAvailable(String ssid, String bssid);
	}

	public void getLocation(LocationManagerCallback callback) {
		Log.i("IMPORTANT", "Got a current location request");
		this.wifiController.requestCurrentLocation(callback);
		this.gpsController.requestCurrentLocation(callback);
	}
}